package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.stark.idiom.lib.model.bean.Idiom;
import com.stark.idiom.lib.model.db.IdiomDbHelper;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityIdiomBinding;
import flc.ast.utils.Content;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;

public class IdiomActivity extends BaseAc<ActivityIdiomBinding> {

    //猜成语
    public static int level = 0;//选择的关卡
    public static List<Idiom> listAll = new ArrayList<>();
    private int rightNum;
    private Dialog myRightDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_idiom;
    }

    @Override
    protected void initView() {
        mDataBinding.ivIdiomBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rlIdiomOptionA.setOnClickListener(this);
        mDataBinding.rlIdiomOptionB.setOnClickListener(this);
        mDataBinding.rlIdiomOptionC.setOnClickListener(this);
        mDataBinding.rlIdiomOptionD.setOnClickListener(this);
        //回答正确弹框
        rightDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.rlIdiomOptionA://选项A
                isRight(1);
                break;
            case R.id.rlIdiomOptionB://选项B
                isRight(2);
                break;
            case R.id.rlIdiomOptionC://选项C
                isRight(3);
                break;
            case R.id.rlIdiomOptionD://选项D
                isRight(4);
                break;
            //回答正确弹框
            case R.id.ivDialogRightNext://下一题
                myRightDialog.dismiss();
                passLevel();
                break;
        }
    }

    @Override
    protected void initData() {
        getLevel();
    }

    //获取关卡
    private void getLevel() {
        List<Integer> list = new ArrayList<>();
        IdiomDbHelper.get(list, 4, 80, 100, new IRetCallback<List<Idiom>>() {
            @Override
            public void onResult(List<Idiom> result) {
                if (result != null && result.size() > 0) {
                    listAll.addAll(result);
                    //分页设置关卡数据
                    setData();
                }
            }
        });
    }

    //设置游戏数据
    private void setData() {
        mDataBinding.tvIdiomLevel.setText(getString(R.string.problem_num, level));
        mDataBinding.tvIdiomProblem.setText(listAll.get(level).getExplanation());
        List<Idiom> list = RandomUtil.randomGetItems(listAll, listAll.get(level), 3);
        mDataBinding.tvIdiomOptionA.setText(list.get(0).getWord());
        mDataBinding.tvIdiomOptionB.setText(list.get(1).getWord());
        mDataBinding.tvIdiomOptionC.setText(list.get(2).getWord());
        mDataBinding.tvIdiomOptionD.setText(list.get(3).getWord());
        boolean isA = checkAnswer(list.get(0).getWord(), listAll.get(level).getWord());
        boolean isB = checkAnswer(list.get(1).getWord(), listAll.get(level).getWord());
        boolean isC = checkAnswer(list.get(2).getWord(), listAll.get(level).getWord());
        boolean isD = checkAnswer(list.get(3).getWord(), listAll.get(level).getWord());
        if (isA) {
            rightNum = 1;
        }
        if (isB) {
            rightNum = 2;
        }
        if (isC) {
            rightNum = 3;
        }
        if (isD) {
            rightNum = 4;
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }

    //判断对错
    private void isRight(int kind) {
        if (kind == rightNum) {
            myRightDialog.show();
        } else {
            mDataBinding.tvIdiomError.setVisibility(View.VISIBLE);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mDataBinding.tvIdiomError.setVisibility(View.INVISIBLE);
                }
            }, 500);
        }
    }

    //过关
    private void passLevel() {
        //判断是否需要解锁下一关
        if (level < (listAll.size())) {//如果当前关卡小于总关卡数
            int spLevel = SPUtil.getInt(mContext, Content.Idiom_Game, 1);//获取记录通过关卡数
            if (level == spLevel) {//如果通过的是未解锁关卡则解锁下一关
                SPUtil.putInt(mContext, Content.Idiom_Game, spLevel + 1);
            }
            level++;
        }
        setData();
    }

    //回答正确弹框
    private void rightDialog() {
        myRightDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_right, null);
        myRightDialog.setContentView(inflate);
        myRightDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myRightDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogNext = inflate.findViewById(R.id.ivDialogRightNext);
        dialogNext.setOnClickListener(this);
    }

}